package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.model.WoodType;
import com.wanglibao.woodtrade.woodtrade.model.WoodTypeDetail;
import com.wanglibao.woodtrade.woodtrade.model.WoodTypeDetailThreePlace;
import com.wanglibao.woodtrade.woodtrade.model.impl.WoodTypeDetailImpl;
import com.wanglibao.woodtrade.woodtrade.model.impl.WoodTypeDetailThreePlaceImpl;
import com.wanglibao.woodtrade.woodtrade.model.impl.WoodTypeImpl;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.ShopFragmentWoodDetailTypeAdapter;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.ShopFragmentWoodTypeAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FilterActivity extends Activity {

    @Bind(R.id.ll_activity_filter_title)
    LinearLayout llActivityFilterTitle;
    @Bind(R.id.edit_activity_filter_search)
    EditText editActivityFilterSearch;

    @Bind(R.id.ll_activity_filter_filter)
    LinearLayout llActivityFilterFilter;
    @Bind(R.id.list_activity_filter_woodtype)
    ListView listActivityFilterWoodtype;
    @Bind(R.id.list_activity_filter_woodTypeDetail)
    ListView listActivityFilterWoodTypeDetail;
    @Bind(R.id.list_activity_filter_place)
    ListView list_activity_filter_place;

    List<WoodType> woodTypeList = new ArrayList<>();
    List<WoodTypeDetail> woodTypeDetailList = new ArrayList<>();
    List<WoodTypeDetailThreePlace> woodTypeDetailThreePlaceList = new ArrayList<>();
    WoodType currentWoodType;
    WoodTypeDetail currentWoodTypeDetail;
    WoodTypeDetailThreePlace currentWoodTypeDetailThreePlace;
    TextView woodTypeLastSelectView, woodTypeDetailLastSelectView, woodTypeDetailThreePlaceLastSelectView;

    ShopFragmentWoodDetailTypeAdapter shopFragmentWoodDetailTypeAdapter;
    ShopFragmentWoodTypeAdapter shopFragmentWoodDetailThreePlaceTypeAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            llActivityFilterTitle.setPadding(0, 0, 0, 0);
        }
        initView();
    }

    private void initView() {

        woodTypeList = new WoodTypeImpl(this).getAll();
        final WoodType woodType = new WoodType("-1","全部");
        woodTypeList.add(0,woodType);
        listActivityFilterWoodtype.setAdapter(new ShopFragmentWoodTypeAdapter(this, WoodType.arrayListTurnArray(woodTypeList)));
        listActivityFilterWoodtype.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (woodTypeDetailLastSelectView != null){
                    woodTypeDetailLastSelectView.setBackgroundDrawable(getResources().getDrawable(R.drawable.ripple_background_type_one_o));
                    woodTypeDetailLastSelectView = null;
                }
                if (woodTypeDetailThreePlaceLastSelectView != null){
                    woodTypeDetailThreePlaceLastSelectView.setBackgroundDrawable(getResources().getDrawable(R.drawable.ripple_background_type_one_d));
                    woodTypeDetailThreePlaceLastSelectView = null;
                }
                currentWoodType = woodTypeList.get(position);
                if (woodTypeLastSelectView != null) {
                    woodTypeLastSelectView.setBackgroundDrawable(getResources().getDrawable(R.drawable.ripple_background_type_one_d));
                }
                view.setBackgroundDrawable(getResources().getDrawable(R.drawable.ripple_background_type_one_o));
                woodTypeLastSelectView = (TextView) view;

                if ("-1".equals(currentWoodType.id)){
                    Intent intent = new Intent();
                    intent.putExtra("type", "search");
                    intent.putExtra("keyword", "");
                    setResult(10, intent);
                    finish();
                }else if("其他".equals(currentWoodType.name)){
                    Intent intent = new Intent();
                    intent.putExtra("type", "filter");
                    intent.putExtra("woodTypeId", currentWoodType.id);
                    intent.putExtra("woodTypeDetailId", "");
                    intent.putExtra("woodTypeDetailName", "其他");
                    setResult(10, intent);
                    finish();
                }else{
                    woodTypeDetailList = new WoodTypeDetailImpl(FilterActivity.this).getWoodTypeDetailByWoodTypeId(currentWoodType.id);
                    shopFragmentWoodDetailTypeAdapter.update(WoodTypeDetail.arrayListTurnArray(woodTypeDetailList));

                    woodTypeDetailThreePlaceList = new WoodTypeDetailThreePlaceImpl(FilterActivity.this)
                            .getWoodTypeDetailThreePlaceByWoodTypeId(woodTypeDetailList.get(0).id);
                    shopFragmentWoodDetailThreePlaceTypeAdapter.update(WoodTypeDetailThreePlace.arrayListTurnArray(woodTypeDetailThreePlaceList));
                }

            }
        });

        woodTypeDetailList = new WoodTypeDetailImpl(this).getWoodTypeDetailByWoodTypeId(woodTypeList.get(1).id);
        shopFragmentWoodDetailTypeAdapter = new ShopFragmentWoodDetailTypeAdapter(this, WoodTypeDetail.arrayListTurnArray(woodTypeDetailList));
        listActivityFilterWoodTypeDetail.setAdapter(shopFragmentWoodDetailTypeAdapter);
        listActivityFilterWoodTypeDetail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (woodTypeDetailThreePlaceLastSelectView != null){
                    woodTypeDetailThreePlaceLastSelectView.setBackgroundDrawable(getResources().getDrawable(R.drawable.ripple_background_type_one_d));
                    woodTypeDetailThreePlaceLastSelectView = null;
                }
                currentWoodTypeDetail = woodTypeDetailList.get(position);
                if (currentWoodType == null) {
                    currentWoodType = new WoodTypeImpl(FilterActivity.this).getById(currentWoodTypeDetail.parent_id);
                }
                if (woodTypeDetailLastSelectView != null) {
                    woodTypeDetailLastSelectView.setBackgroundDrawable(getResources().getDrawable(R.drawable.ripple_background_type_one_o));
                }
                view.setBackgroundDrawable(getResources().getDrawable(R.drawable.ripple_background_type_one_d));
                woodTypeDetailLastSelectView = (TextView) view;
                //返回上一页
                woodTypeDetailThreePlaceList = new WoodTypeDetailThreePlaceImpl(FilterActivity.this)
                        .getWoodTypeDetailThreePlaceByWoodTypeId(currentWoodTypeDetail.id);
                shopFragmentWoodDetailThreePlaceTypeAdapter.update(WoodTypeDetailThreePlace.arrayListTurnArray(woodTypeDetailThreePlaceList));

            }
        });

        woodTypeDetailThreePlaceList = new WoodTypeDetailThreePlaceImpl(this).getWoodTypeDetailThreePlaceByWoodTypeId(woodTypeDetailList.get(0).id);
        shopFragmentWoodDetailThreePlaceTypeAdapter = new ShopFragmentWoodTypeAdapter(this,WoodTypeDetailThreePlace.arrayListTurnArray(woodTypeDetailThreePlaceList));
        list_activity_filter_place.setAdapter(shopFragmentWoodDetailThreePlaceTypeAdapter);
        list_activity_filter_place.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                currentWoodTypeDetailThreePlace = woodTypeDetailThreePlaceList.get(position);
                if (currentWoodTypeDetail == null) {
                    currentWoodTypeDetail = new WoodTypeDetailImpl(FilterActivity.this).getById(currentWoodTypeDetailThreePlace.parent_id);
                }
                if (woodTypeDetailThreePlaceLastSelectView != null) {
                    woodTypeDetailThreePlaceLastSelectView.setBackgroundDrawable(getResources().getDrawable(R.drawable.ripple_background_type_one_d));
                }
                view.setBackgroundDrawable(getResources().getDrawable(R.drawable.ripple_background_type_one_o));
                woodTypeDetailThreePlaceLastSelectView = (TextView) view;

                Intent intent = new Intent();
                intent.putExtra("type", "filter");
                intent.putExtra("woodTypeId", currentWoodType.id);
                intent.putExtra("woodTypeDetailId", currentWoodTypeDetail.id);
                intent.putExtra("woodTypeDetailName", currentWoodTypeDetail.name);
                intent.putExtra("place_id", currentWoodTypeDetailThreePlace.id);
                intent.putExtra("place_name", currentWoodTypeDetailThreePlace.name);
                setResult(10, intent);
                finish();
            }
        });

        listActivityFilterWoodtype.post(new Runnable() {
            @Override
            public void run() {
                currentWoodType = woodTypeList.get(1);
                TextView textView = (TextView) listActivityFilterWoodtype.getChildAt(1);
                woodTypeLastSelectView = textView;
                woodTypeLastSelectView.setBackgroundDrawable(getResources().getDrawable(R.drawable.ripple_background_type_one_o));
            }
        });

    }

    @OnClick(R.id.imgbtn_activity_filter_back)
    void onImgbtnActivityFilterBackClick() {
        onBackPressed();
    }

    @OnClick(R.id.tv_activity_filter_search)
    void onTextViewActivityFilterSearchClick() {
        String value = editActivityFilterSearch.getText().toString();
        Intent intent = new Intent();
        intent.putExtra("type", "search");
        intent.putExtra("keyword", value);
        setResult(10, intent);
        finish();
    }

}
